<?php

/**
 * This is the model class for table "{{members_sunsingle}}".
 *
 * The followings are the available columns in table '{{members_sunsingle}}':
 * @property integer $id
 * @property string $ordersn
 * @property integer $status
 * @property integer $add_time
 * @property integer $update_time
 * @property integer $uid
 * @property string $experience
 */
class MembersSunsingleModel extends BaseModel
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{members_sunsingle}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ordersn, uid', 'required'),
			array('ordersn', 'match', 'pattern'=>'/^[ A-Za-z0-9]+$/u','message'=>'订单不合法！'),			
			array('ordersn','unique', 'message' => '已经存在此订单号'),
			array('uid', 'numerical', 'integerOnly'=>true),
			array('status, add_time, update_time, experience', 'safe'),
			array('id, ordersn, status, add_time, update_time, uid, experience', 'safe', 'on'=>'search'),
		);
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'ordersn' => '订单号',
			'status' => 'Status',
			'add_time' => 'Add Time',
			'update_time' => 'Update Time',
			'uid' => 'Uid',
			'experience' => '心得',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ordersn',$this->ordersn,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('add_time',$this->add_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('experience',$this->experience,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function createSearchCriteria(){
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ordersn',$this->ordersn,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('add_time',$this->add_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('experience',$this->experience,true);

		return 	$criteria;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MembersSunsingleModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave(){
		$this->update_time = time();//修改时间
		$this->experience = CHtml::encode($this->experience);//心得
		if ($this->isNewRecord){
			$this->status = 0;//处理状态
			$this->add_time = $this->update_time;//添加时间
		}
		return true;
	}
	/**
	 * 晒单,组成title,content,pid,等信息后再晒单
	 * @param  [type] $objSalesModel [description]
	 * @return [type]                [description]
	 */
	public function post($objSalesModel, $userInfo = ''){
		if(!is_array($userInfo) && !empty($userInfo)){
			$userInfo = MembersModel::model()->getUserInfo($userInfo);
		}
		empty($userInfo) ? $userInfo = yii::app()->user->getInfo() : '';

		//先找一下看是否是积分的产品,如果找不到则去商品表里面查找
		$objRedeemModel = RedeemModel::model()->find(array('condition' => 'product_id=:product_id', 'params' => array('product_id' => $objSalesModel->product_id)));
		if($objRedeemModel){
			$subject = "{$objRedeemModel->title}【{$objSalesModel->shop_name}】";
			$url = Yii::app()->request->hostInfo.yii::app()->createUrl('/front/good/redeemskip', array('gid' => $objRedeemModel->id));
			$url = "[url=$url]我也要买[/url]";
			$message = "[size=3][b]产品名称[/b]：{$objRedeemModel->title}\n[img=100,100]{$objRedeemModel->thumb}[/img]\n$url\n[b]订单号[/b]：{$this->ordersn}\n[b]抢购价格[/b]：[color=Red][b][i]{$objSalesModel->promo_price}元[/i][/b][/color]\n[b]购买心得[/b]:{$this->experience}[/size]";			
		}else{
			$GoodsModel = $objSalesModel->GoodsModel;
			if(!$GoodsModel || $GoodsModel->product_id != $objSalesModel->product_id){
				//找不到这商品模型，或者 通过goods_id找出来的,两个的商品ID不一样，说明信息有误
				return false;
			}
			$subject = "{$GoodsModel->goods_name}【{$objSalesModel->shop_name}】";
			$url = Yii::app()->request->hostInfo.yii::app()->createUrl('/front/good/skip', array('gid' => $GoodsModel->goods_id));
			$url = "[url=$url]我也要买[/url]";
			$message = "[size=3][b]产品名称[/b]：{$GoodsModel->goods_name}\n[img=100,100]{$GoodsModel->product_img}[/img]\n$url\n[b]订单号[/b]：{$this->ordersn}\n[b]抢购价格[/b]：[color=Red][b][i]{$objSalesModel->promo_price}元[/i][/b][/color]\n[b]购买心得[/b]:{$this->experience}[/size]";
		}
		
		

		$fid = 55;
		$typeid = 3;//主题类别 积分晒单或其它晒单
		$arrData = array(
			'subject' => $subject,
			'message' => $message,
			'fid' => $fid,
			'typeid' => $typeid,						
		);
		//晒单后送积分
		yii::import('ext.yzwAPI.*');
		if(DzAPI::getInstance($userInfo)->post($arrData)){
			if(MembersModel::model()->plusScore($userInfo['uid'], 15)){
				MembersActionModel::model()->addShareAction($userInfo['uid'], '晒单送积分15个');
			}			
			return true;
		}else{
			return false;
		}
	}
	
	public static function getStatusHtmlList(){
		return array(
			0 => '未处理',
			1 => '已晒单',
			2 => '找不到记录已取消',
 		);
	}
	public static function getStatusHtmlDetail($data){
		$arrData = self::getStatusHtmlList();
		return $arrData[$data];
	}
}
